<template>
	<view>
		<view v-if="collectList.length === 0" class="empty-text">暂无收藏</view>
		<scroll-view class="collect-list" scroll-y="true" :style="'height:' + scrollViewHeight + '1000px'">
			<view v-for="(item, index) in collectList" :key="index" class="collect-item">
				<image :src="item.img_path" class="item-img"></image>
				<view class="item-info">
					<text>{{ item.name }}</text>
					<text class="item-price">${{ item.discount_price }}</text>
				</view>
				<view class="item-action">
					<button @click="removeCollect(item)">取消收藏</button>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		ListFavorite,
		DeleteFavorite
	} from "@/api/favorites.js"
	export default {
		data() {
			return {
				scrollViewHeight: 0,
				collectList: []
			};
		},
		mounted() {
			this.getList()
		},
		methods: {
			async getList() {
				let res = await ListFavorite({})
				this.$set(this.collectList = res.data.item)
			},
			async removeCollect(item) {
				let res = await DeleteFavorite({
					'id': item.product_id,
				})
				if (res.status == 200) {
					this.getList()
				}
			}
		}
	};
</script>

<style>
	.collect-list {
		flex: 1;
		background-color: #fff;
		padding: 10px;
	}

	.collect-item {
		display: flex;
		align-items: center;
		margin-bottom: 10px;
		padding: 10px;
		border: 1px solid #ccc;
		border-radius: 5px;
	}

	.item-img {
		width: 80px;
		height: 80px;
		margin-right: 10px;
		object-fit: cover;
	}

	.item-info {
		flex: 1;
	}

	.item-price {
		color: red;
		font-size: 16px;
		margin-top: 5px;
	}

	.item-action {
		display: flex;
		justify-content: flex-end;
	}

	button {
		padding: 6px 12px;
		background-color: #f0f0f0;
		border: none;
		border-radius: 4px;
	}
</style>